package shopping.mangebean;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import shopping.model.Account;
import shopping.remote.IAccountRemote;


@ManagedBean(name="login")
@SessionScoped
public class LoginBean {
	private String usserName;
	private String password;
	private int permistion;
	private String message;
	
	public String getUsserName() {
		return usserName;
	}

	public void setUsserName(String usserName) {
		this.usserName = usserName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public LoginBean() {
		message = null;
		permistion = -1;
	}
	
	public int getPermistion() {
		return permistion;
	}

	public void setPermistion(int permistion) {
		this.permistion = permistion;
	}

	public String login() throws NamingException{
		Context context = new InitialContext();
		IAccountRemote accountRemote = (IAccountRemote) context.lookup("ejb/AccountStateless");
		Account account = accountRemote.getAccount(usserName, password);
		if(account == null){
			message = "User name or password not found";
			return null;
		}
		permistion = account.getPermission();
		return "index";
	}
}
